//
//  PullDownMenu.m
//  PigMarket
//
//  Created by mac on 2019/5/27.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PullDownMenu.h"
#import "PullDownMenuCell.h"

@interface PullDownMenu ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic , strong) NSMutableArray *dataArr;
@property (nonatomic, assign) PullDownType type;
/**
 topSpace
 */
@property (nonatomic , assign) CGFloat topSpace;
@end

static PullDownMenu *instance = nil;
@implementation PullDownMenu

#pragma mark - init method

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[PullDownMenu alloc] init];
    });
    return instance;
}
- (void)showWithFrame:(CGRect)frame type:(PullDownType)type dataArr:(NSMutableArray *)dataArr completionBlock:(void (^)(NSString *, NSInteger , PullDownType))completionBlock{
    self.frame = frame;
    self.type  = type;
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:dataArr];
    [self addSubview:self.myTableView];
    [[UIApplication sharedApplication].keyWindow addSubview:self];

    
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
   
    [UIView animateWithDuration:0.5 animations:^{
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.myTableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
        [self.myTableView reloadData];
    }];
    self.selectItemBlock = completionBlock;
}
#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PullDownMenuCell *cell = [PullDownMenuCell cellWithTableView:tableView];
    if (self.dataArr.count > 0 && self.dataArr.count > indexPath.row) {
        cell.model = self.dataArr[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
//    __block NSInteger currentIdx = 0;
//    [self.dataArr enumerateObjectsUsingBlock:^(PullDownMenuModel *model, NSUInteger idx, BOOL * _Nonnull stop) {
//        if (indexPath.row == idx) {
//            model.isSelect = YES;
//            currentIdx = idx;
//        } else {
//            model.isSelect = NO;
//        }
//    }];
    PullDownMenuModel *model = self.dataArr[indexPath.row];
    
    
    if (self.selectItemBlock) {
    
        self.selectItemBlock(model.title, indexPath.row, PullDownTypeTransicationTypes);
//        if (self.areaType == AreaTypeTribe) {
//            self.selectAreaBlock(model.tribeName,model.tribeId,self.areaType);
//        }else {
//            self.selectAreaBlock(model.regionName,model.parentId,self.areaType);
//        }
        
    }
    
    [self hide];
}

#pragma mark - target action

#pragma mark - private method
- (void)hide {
    [UIView animateWithDuration:0.3 animations:^{
        self.myTableView.frame =  CGRectMake(0, 0, SCREEN_WIDTH, 0);
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.0];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0) style:UITableViewStylePlain];
        _myTableView.rowHeight = 50;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.tableFooterView = [[UIView alloc] init];
        _myTableView.bounces = NO;
        _myTableView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.0];
    }
    return _myTableView;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

@end
